﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ivony.Fluent;
using Ivony.Html;
using Ivony.Html.Parser;

using ihongma.Common;
namespace ihongma.Job
{
    public class Jobu148
    {
        ihongma.BLL.article add = new ihongma.BLL.article();
        JumonyParser parser = new JumonyParser();
        public void url(int j)
        {
            Random seed = new Random();
            //StringBuilder sb = new StringBuilder();
            string showhtml = HttpRequestHelper.GetPageSourceByUrl("http://www.u148.net/audio/" + j.ToString() + ".html", "utf-8");
            var dataDocument = parser.Parse(showhtml);

            dataDocument.Find(".summary .level4").Remove();
            dataDocument.Find(".summary .comments").Remove();
            dataDocument.Find(".u148content h1 .link1").Remove();
            var date = dataDocument.Find(".summary span").ToArray();

            var items = dataDocument.Find(".u148content h1 a").ToArray();
            // Response.Write(items.Count().ToString() + "dsd" + date.Count());
            for (int i = 0; i < items.Count(); i++)
            {
               
                if (date[i].InnerHtml() != "")
                {
                    // Response.Write("http://www.u148.net/" + items[i].Attribute("href").Value());
                    pageinfo("http://www.u148.net" + items[i].Attribute("href").Value(), date[i].InnerHtml().Replace("发布于", "")+" "+DateTime.Now.AddHours(-seed.Next(12)).ToLongTimeString(), items[i].InnerHtml());
                }
                else
                {
                    pageinfo("http://www.u148.net" + items[i].Attribute("href").Value(), DateTime.Now.ToString(), items[i].InnerHtml());
                }
                //href.Count()
            }
            //foreach (var i in date.ToArray())
            //{
            //    Response.Write(i.InnerHtml().Replace("发布于", "") + "<br/>");
            //}


        }
        public string ret(string str)
        {
            string temp = str;
            if (str.Contains("http://file3.u148.net"))
            {
                temp = temp.Replace("http://file3.u148.net", "/tools/imgdow.ashx?URL=http://file3.u148.net");
            }

            if (str.Contains("http://file1.u148.net"))
            {
                temp = temp.Replace("http://file2.u148.net", "/tools/imgdow.ashx?URL=http://file2.u148.net");
            }
            if (str.Contains("http://file1.u148.net"))
            {
                temp = temp.Replace("http://file1.u148.net", "/tools/imgdow.ashx?URL=http://file1.u148.net");
            }
            return temp;
        }
        public void pageinfo(string usrl, string data,string title)
        {
            if (!add.ExistsTitle(title))
            {
                //StringBuilder sb = new StringBuilder();
                string showhtml = HttpRequestHelper.GetPageSourceByUrl(usrl, "utf-8");
                var dataDocument = parser.Parse(showhtml);
                var items = dataDocument.Find(".u148content");
                var conunt = ret(items.Find(".content").Single().ToString().Replace(usrl, "#").Replace("来自有意思吧（www.u148.net）", "来自鸿马网（www.ihongma.com）").Replace("www.u148.net", "www.ihongma.com"));
                var titele = items.Find("h1 .link2").Single().InnerText();
                var img = "";
                var temp = dataDocument.Find("meta[name='Description']").Single().Attribute("content").Value();
                try
                {
                    img = "/tools/imgdow.ashx?URL=" + items.Find(".content img").ElementAt(0).Attribute("src").Value();
                }
                catch (Exception)
                {

                    img = "/upload/null.jpg";
                }

                ihongma.Model.article ar = new ihongma.Model.article()
                {
                    add_time = DateTime.Parse(data),
                    author = "colt",
                    category_id = 353,
                    channel_id = 1,
                    zhaiyao = temp,
                    click = 100,
                    content = conunt,
                    //dataDocument.Find("#artical_real p").ElementAt(0).InnerHtml() + "<p>" + temp + "</p>",

                    title = titele,
                    digg_act = 0,
                    digg_good = 0,
                    form = "鸿马",
                    img_url = img,
                    is_hot = 0,
                    is_lock = 0,
                    is_msg = 0,
                    is_red = 0,
                    is_slide = 0,
                    is_top = 0,
                    seo_description = temp,
                    seo_keywords = titele,
                    seo_title = titele,
                    sort_id = 99,
                    link_url = ""

                };
                add.Add(ar);
            }
        }
        public void u148(int count)
        {
            for (int i = 0; i <= count; i++)
            {
                url(i);
            }
        }
    }
}
